//
//  AiRoleItemView.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2025/4/1.
//

import SwiftUI
import Kingfisher

/**
 *  api 站点 item
 */
struct ApiCustomItemView: View {
    @StateObject var viewModel = AiCustomItemViewModel()

    var bean:AiCustomBean

    var body: some View {
        // 目前contextMenu 会失效，先不处理，后续看下有没有更好的解决方案
        ZStack {
            Menu {
                moreContent
            } label: {
                contentView
            }
        }.contextMenu {
            moreContent
        }
    }
    
    
}

//内容试图
extension ApiCustomItemView {
    private var contentView: some View {
        HStack(alignment:.center) {
            Text((bean.name ?? "")?.prefix(1) ?? "")
                .font(.system(size: 20, weight: .medium))
                .foregroundColor(.white)
                .frame(width: 50, height: 50)
                .background(themeManager.themeColor)
                .clipShape(.circle)
            VStack(alignment:.leading) {
                if viewModel.refreshTag>=0{
                    FlexboxLayout(horizontalSpacing: 5, verticalSpacing: 5) { // 可调整间距
                        Text(bean.name ?? "")
                            .font(.system(size: 16, weight: .medium))
                            .foregroundColor(.bgNormalText)
                            .lineLimit(1)
                        if AiCustomUtils.shared.isVisibility(type: IAiModelType.chat.rawValue, objectId: bean.objectId ?? "") {
                            Text("聊天").font(.system(size: 10)).foregroundColor(Color.white).lineLimit(2)
                                .padding(.horizontal, 2)
                                .padding(.vertical, 1)
                                .background(themeManager.themeColor)
                                .clipShape(RoundedRectangle(cornerRadius: 3))
                        }
                        if AiCustomUtils.shared.isVisibility(type: IAiModelType.diaryReply.rawValue, objectId: bean.objectId ?? "") {
                            Text("评论").font(.system(size: 10)).foregroundColor(Color.white).lineLimit(2)
                                .padding(.horizontal, 2)
                                .padding(.vertical, 1)
                                .background(themeManager.themeColor)
                                .clipShape(RoundedRectangle(cornerRadius: 3))
                        }
                        if AiCustomUtils.shared.isVisibility(type: IAiModelType.aiDiary.rawValue, objectId: bean.objectId ?? "") {
                            Text("日记").font(.system(size: 10)).foregroundColor(Color.white).lineLimit(2)
                                .padding(.horizontal, 2)
                                .padding(.vertical, 1)
                                .background(themeManager.themeColor)
                                .clipShape(RoundedRectangle(cornerRadius: 3))
                        }
                        if AiCustomUtils.shared.isVisibility(type: IAiModelType.weeklyLetter.rawValue, objectId: bean.objectId ?? "") {
                            Text("来信").font(.system(size: 10)).foregroundColor(Color.white).lineLimit(2)
                                .padding(.horizontal, 2)
                                .padding(.vertical, 1)
                                .background(themeManager.themeColor)
                                .clipShape(RoundedRectangle(cornerRadius: 3))
                        }
                        if AiCustomUtils.shared.isVisibility(type: IAiModelType.call.rawValue, objectId: bean.objectId ?? "") {
                            Text("语音").font(.system(size: 10)).foregroundColor(Color.white).lineLimit(2)
                                .padding(.horizontal, 2)
                                .padding(.vertical, 1)
                                .background(themeManager.themeColor)
                                .clipShape(RoundedRectangle(cornerRadius: 3))
                        }
                        if AiCustomUtils.shared.isVisibility(type: IAiModelType.image.rawValue, objectId: bean.objectId ?? "") {
                            Text("图片").font(.system(size: 10)).foregroundColor(Color.white).lineLimit(2)
                                .padding(.horizontal, 2)
                                .padding(.vertical, 1)
                                .background(themeManager.themeColor)
                                .clipShape(RoundedRectangle(cornerRadius: 3))
                        }
                    }
                }
                Text(bean.model ?? "")
                    .font(.system(size: 14))
                    .foregroundColor(.bgNormalText4).lineLimit(2)
                    .multilineTextAlignment(.leading)  // 明确设置多行文本对齐方式
            }
            Spacer()
            Menu {
                moreContent
            } label: {
                Image(.homeIconMore2)
                    .resizable()
                    .aspectRatio(contentMode: .fill)
                    .padding(5)
                    .frame(width: 30,height: 30)
                    .contentShape(Rectangle())
                    .onTapGesture {
                        
                    }
            }
            
        }.frame(maxWidth: .infinity,alignment:.leading)
            .padding(EdgeInsets(top: 12, leading: 15, bottom: 12, trailing: 5))
            .background(.bgItem)
            .cornerRadius(15)
    }
}

//菜单
extension ApiCustomItemView {
    // 选择
    private var commonMenuContent: some View {
        let cancelDiaryReply = AiCustomUtils.shared.isVisibility(type: IAiModelType.diaryReply.rawValue, objectId: bean.objectId ?? "")
        let cancelChat = AiCustomUtils.shared.isVisibility(type: IAiModelType.chat.rawValue, objectId: bean.objectId ?? "")
        let cancelAiDiary = AiCustomUtils.shared.isVisibility(type: IAiModelType.aiDiary.rawValue, objectId: bean.objectId ?? "")
        let cancelWeeklyLetter = AiCustomUtils.shared.isVisibility(type: IAiModelType.weeklyLetter.rawValue, objectId: bean.objectId ?? "")
        let cancelImage = AiCustomUtils.shared.isVisibility(type: IAiModelType.image.rawValue, objectId: bean.objectId ?? "")
        return Group {
            Button("\(cancelDiaryReply ? "取消" : "")评论"){
                let type = IAiModelType.diaryReply.rawValue
                if cancelDiaryReply {
                    AiCustomUtils.shared.addAiCustomMode(type: type, value: "")
                    AiCustomUtils.shared.addTemBean(type: type, bean: nil)
                } else {
                    AiCustomUtils.shared.addAiCustomMode(type: type, value: bean.objectId ?? "")
                    AiCustomUtils.shared.addTemBean(type: type, bean: bean)
                }
                viewModel.doRefreshTag()
            }
            Button("\(cancelChat ? "取消" : "")聊天"){
                let type = IAiModelType.chat.rawValue
                if cancelChat {
                    AiCustomUtils.shared.addAiCustomMode(type: type, value: "")
                    AiCustomUtils.shared.addTemBean(type: type, bean: nil)
                } else {
                    AiCustomUtils.shared.addAiCustomMode(type: type, value: bean.objectId ?? "")
                    AiCustomUtils.shared.addTemBean(type: type, bean: bean)
                }
                viewModel.doRefreshTag()
            }
            Button("\(cancelAiDiary ? "取消" : "")日记") {
                let type = IAiModelType.aiDiary.rawValue
                if cancelAiDiary {
                    AiCustomUtils.shared.addAiCustomMode(type: type, value: "")
                    AiCustomUtils.shared.addTemBean(type: type, bean: nil)
                } else {
                    AiCustomUtils.shared.addAiCustomMode(type: type, value: bean.objectId ?? "")
                    AiCustomUtils.shared.addTemBean(type: type, bean: bean)
                }
                viewModel.doRefreshTag()
            }
            Button("\(cancelWeeklyLetter ? "取消" : "")来信") {
                let type = IAiModelType.weeklyLetter.rawValue
                if cancelWeeklyLetter {
                    AiCustomUtils.shared.addAiCustomMode(type: type, value: "")
                    AiCustomUtils.shared.addTemBean(type: type, bean: nil)
                } else {
                    AiCustomUtils.shared.addAiCustomMode(type: type, value: bean.objectId ?? "")
                    AiCustomUtils.shared.addTemBean(type: type, bean: bean)
                }
                viewModel.doRefreshTag()
            }
            Button("\(cancelImage ? "取消" : "")图片") {
                let type = IAiModelType.image.rawValue
                if cancelImage {
                    AiCustomUtils.shared.addAiCustomMode(type: type, value: "")
                    AiCustomUtils.shared.addTemBean(type: type, bean: nil)
                } else {
                    AiCustomUtils.shared.addAiCustomMode(type: type, value: bean.objectId ?? "")
                    AiCustomUtils.shared.addTemBean(type: type, bean: bean)
                }
                viewModel.doRefreshTag()
            }
        }
    }
    //更多
    private var moreContent: some View {
        Group {
            Button("网站跳转"){
                guard let url = URL(string: bean.url ?? "") else {
                    showToast("暂未配置网站地址")
                    return
                }
                // 打开 Safari
                UIApplication.shared.open(url)
            }
            Button("错误日志"){
                pathManager.jump(type: JumpType.AI_CUSTOM_ERROR,data: bean)
            }
            Button("编辑") {
                pathManager.jump(type: JumpType.AI_CUSTOM_CREATE, data: bean)
            }
            Menu {
                commonMenuContent
            } label: {
                Button("选择") {
                    
                }
            }
            
        }
    }
}

//定义预览提供者
struct ApiCustomItemView_Previews: PreviewProvider {
    static var previews: some View {
        configurePreviewEnvironment(for: ApiCustomItemView(bean: getBean()))
    }
    
    static func getBean() -> AiCustomBean {
        let bean = AiCustomBean()
        bean.name = "1111111111178"
        return bean
    }
}
